﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;
using System.Threading;

public partial class SystemManage_TruckStatusList : BasePage
{
    ISyTruckStatusHandler ttManager = new SyTruckStatusHandler();
    DataBase db = new DataBase();

    private static bool isUpdate = false;
    private static bool isDel = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1027"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
                else
                {
                    //判断用户有没有添加的权限
                    if (strPermission.Contains("1028"))
                    {
                        lblTruckStatusAdd.Visible = true;
                    }
                    else
                    {
                        lblTruckStatusAdd.Visible = false;
                    }
                    //判断用户有没有修改的权限
                    if (strPermission.Contains("1032"))
                    {
                        isUpdate = true;
                    }
                    else
                    {
                        isUpdate = false;
                    }
                    //判断用户有没有删除的权限
                    if (strPermission.Contains("1033"))
                    {
                        isDel = true;
                    }
                    else
                    {
                        isDel = false;
                    }
                }

                //CheckPagePermission();//判断当前用户有没有添加删除更新的权限

            }
           
            ViewState["OrderBy"] = "PKID";
            ViewState["Accending"] = "asc";
            dataBind();
        }
    }
   

    private void dataBind()
    {
        string sqlStr = "SELECT * FROM Sy_TruckStatus";

        DataTable dt = ttManager.GetList(sqlStr, null);
        DataView view = dt.DefaultView;
        view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvTTList.DataSource = view;
        gvTTList.DataBind();
        if (dt != null && dt.Rows.Count > 0)
        {
            lblTotal.Text = dt.Rows.Count.ToString();
        }
        else
        {
            lblTotal.Text = "0";
        }
    }
   

    protected void gvTTList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTTList.PageIndex = e.NewPageIndex;
        dataBind();

    }
    protected void gvTTList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (!ttManager.DeleteByID(int.Parse(e.CommandArgument.ToString())))
            {
                Jscript.AlertInAjax(Resources.Messages.DeleteFailed, UpdatePanel1);
            }
        }
        dataBind();

    }
    protected void gvTTList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvTTList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
    protected void gvTTList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnDel = (LinkButton)e.Row.FindControl("lbtnDel");
            Label lblEdit = (Label)e.Row.FindControl("lblUpdate");

            if (isUpdate)
            {
                lblEdit.Visible = true;
            }
            else
            {
                lblEdit.Visible = false;
            }

            if (isDel)
            {
                btnDel.Visible = true;
            }
            else
            {
                btnDel.Visible = false;
            }

            Label lblStatusName = (Label)e.Row.FindControl("lblStatusName");
            Label lblHKStatusName = (Label)e.Row.FindControl("lblHKStatusName");
            Label lblENGStatusName = (Label)e.Row.FindControl("lblENGStatusName");

            if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
            {
                lblStatusName.Visible = false;
                lblHKStatusName.Visible = false;
                lblENGStatusName.Visible = true;
            }
            else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
            {
                lblStatusName.Visible = false;
                lblHKStatusName.Visible = true;
                lblENGStatusName.Visible = false;
            }
            else
            {
                lblStatusName.Visible = true;
                lblHKStatusName.Visible = false;
                lblENGStatusName.Visible = false;
            }
        }
    }

    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvTTList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvTTList.PageCount)
            {
                this.gvTTList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
        }
    }
}
